#!/usr/bin/python3

import time
import hmac
import hashlib
import requests

# 禁用 SSL 警告
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

def generate_signature(method: str, path: str, timestamp: str, secret_key: str) -> str:
    """
    HMAC-SHA256 签名生成算法（与 Go 版本保持完全一致）
    
    参数需保持相同顺序：
    method - HTTP 方法（大写）
    path - 请求路径（含斜杠）
    timestamp - 字符串格式的时间戳
    secret_key - 密钥字符串
    """
    payload = f"{method}\n{path}\n{timestamp}"
    digest = hmac.new(
        key=secret_key.encode('utf-8'),
        msg=payload.encode('utf-8'),
        digestmod=hashlib.sha256
    ).hexdigest()
    return digest

#print(generate_signature("GET", "/test", str(int(time.time())), "sk"))
#print(generate_signature("GET", "/test", "1741154631", "sk"))


#url = "http://localhost:8080/test"
#url = "http://192.168.255.201:8080/test"
#url = "https://localhost:8080/test"
url = "https://192.168.255.201:8080/test"


method = "GET"
path = "/test"
secret = "sk"  # 需要与服务器端匹配的密钥
timestamp = str(int(time.time()))

def main():
    headers = {
        "X-Access-Key": "ak",
        "X-Timestamp": timestamp,
        "X-Signature": generate_signature(method, path, timestamp, secret)
    }

    # http 请求
    #response = requests.get(url, headers=headers)
    #print(f"Status: {response.status_code} {response.reason}")
    #print(response.text)

    # https 请求
    try:
        response = requests.get(url, headers=headers, verify=False)
    except requests.exceptions.SSLError as e:
        print(f"SSL Error: {e}")
        return

    print(f"Status: {response.status_code} {response.reason}")
    print(response.text)



if __name__ == "__main__":
    main()